<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>英雄管理</el-breadcrumb-item>
      <el-breadcrumb-item>英雄列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-table :data="heroList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" width="width"></el-table-column>
        <el-table-column prop="_id" label="ID" width="width"></el-table-column>
        <el-table-column label="英雄名称" width="width" prop="name"></el-table-column>
        <el-table-column label="英雄图片" width="width">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" style="width:50px;height:50px" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="toEditHero(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeHero(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'heroList',
  data() {
    return {
      heroList: []
    }
  },
  methods: {
    // 获取列表数据
    async getEquipList() {
      const res = await this.$http.get('rest/heroes')
      this.heroList = res.data
      console.log(this.heroList)
    },

    afterUpload(res) {
      this.$set(this.equipEditList, 'image', res.url)
    },
    // 删除
    async removeHero(row) {
      const confirm = await this.$confirm(
        `是否删除英雄装备:${row.name}`,
        '删除操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirm === 'confirm') {
        await this.$http.delete(`rest/hero/${row._id}`)
        this.$message.success(`成功删除英雄：${row.name}`)
        this.getEquipList()
      } else {
        this.$message.info('取消删除操作')
      }
    },
    toEditHero(row) {
      this.$message.info('即将跳转到编辑界面')
      this.$router.push(`/heroes/edit/${row._id}`)
      console.log('ok')
    }
  },
  created() {
    this.getEquipList()
  }
}
</script>